﻿using System.Data.SqlClient;
using System.Data.SqlTypes;

public partial class StoredProcedures
{
	[Microsoft.SqlServer.Server.SqlProcedure]
	public static void AddTag(SqlString StationId,SqlString Caster,
		SqlString Tag)
	{
		if (!CheckString(StationId) || !CheckString(Caster) || !CheckString(Tag))
		{
			return;
		}

		using (SqlConnection con = new SqlConnection("context connection=true"))
		{
			con.Open();
			using (CommandSet cmd = new CommandSet(con.CreateCommand(),StationId,Caster,Tag))
			{
				cmd.CommandText = "SELECT dbo.CheckBind(@lid,@tid);";
				int Ret = (int)cmd.ExecuteScalar();

				if (Ret == 1)
				{
					return;
				}


				cmd.CommandText = "SELECT dbo.GetBindedTagsCount(@lid);";
				int BindedCount = (int)cmd.ExecuteScalar();


				if (BindedCount >= MaxTagsCount)
				{
					cmd.CommandText = "EXECUTE dbo.LastValueReleaseBind @lid";
					cmd.ExecuteNonQuery();
				}

				cmd.CommandText = "EXECUTE dbo.RegistBind @lid,@tid;";
				cmd.ExecuteNonQuery();
			}
		}
	}


};
